<template>
  <div class="login-container">
    <div class="login-box">
      <h1 class="login-title">管理员登录</h1>
      <div class="login-form">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" placeholder="密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
        <div class="login-footer">
          <p>还没有账号？<a href="#">联系上级管理员</a></p>
          <p>忘记密码？<a href="#">找回密码</a></p>
        </div>
      </div>
    </div>
  </div>
</template>  
    
<script>
import axios from 'axios';
import router from '@/router';
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {

    async login() {
      try {
        const params = new URLSearchParams({
          username: this.loginForm.username,
          password: this.loginForm.password
        });
        const response = await axios.get('http://localhost:8089/manager/login?' + params.toString());
        if (response.data.code === 200) { // 检查响应码是否为 200 
          // 登录成功，可以跳转到其他页面或执行其他操作  
          router.push('/manage/home');
          let userJson = JSON.stringify(response.data.data);
          localStorage.setItem("superuser", userJson);
          setTimeout(() => {
            localStorage.removeItem("superuser");
          }, 10 * 60 * 1000);
          console.log('Login successful!');
        } else {
          // 登录失败，显示错误信息  
          alert("您的账号或者密码错误，请确保已经注册或输入了正确账号密码！")
          this.error = response.data.message;
        }
      } catch (error) {
        // 请求发生错误，显示通用错误信息  
        this.error = 'An error occurred during login.';
      }
    }
  }
};
</script>  
    
<style scoped>  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f5f7fa;
  }

  .login-box {
    width: 600px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    background-color: #ffffff;
    padding: 40px;
  }

  .login-title {
    text-align: center;
    margin-bottom: 30px;
    color: #333333;
    font-size: 24px;
    font-weight: bold;
  }

  .login-form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

  .login-footer {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .login-footer a {
    color: #409eff;
  }
</style>